package edu.cmu.javadb.plan;

import edu.cmu.javadb.plan.condition.Path;
import edu.cmu.javadb.plan.visitor.Visitor;


public class Join extends PlanNode {

	private PlanNode left;
	private PlanNode right;
	private Path leftColumn = null;
	private Path rightColumn = null;
	
	public Join(PlanNode left, PlanNode right) {
		//this is the natural join
		setLeft(left);
		setRight(right);
	}
	
	public Join(PlanNode left, Path leftColumn, PlanNode right, Path rightColumn) {
		this.leftColumn = leftColumn;
		this.rightColumn = rightColumn;
		setLeft(left);
		setRight(right);
	}
	
	public PlanNode getLeft() {
		return left;
	}

	public PlanNode getRight() {
		return right;
	}
	
	public void setLeft(PlanNode left) {
		this.left = left;
		this.left.setFather(this);
	}

	public void setRight(PlanNode right) {
		this.right = right;
		this.right.setFather(this);
	}	

	public Path getLeftColumn() {
		return leftColumn;
	}

	public Path getRightColumn() {
		return rightColumn;
	}
	
	@Override
	public String toString() {
		return "Join[" + this.getName() + "]: " + leftColumn + " = " + rightColumn  ;
	}

	@Override
	public <T> T accept(Visitor<T> visitor) {
		return visitor.visitNode(this);
	}

	@Override
	public <T> void visitChildren(Visitor<T> visitor) {
		left.accept(visitor);
		right.accept(visitor);
	}

	@Override
	public boolean containsRelation(String name) {
		if (this.name.equals(name))
			return true;
		return left.containsRelation(name) || right.containsRelation(name);
	}

	//this field would be true if this is the place where vertical fragment happen
	private boolean verticalFragmented = false;
	
	public boolean isVerticalFragmented() {
		return verticalFragmented;
	}

	public void setVerticalFragmented(boolean containFragment) {
		this.verticalFragmented = containFragment;
	}
}
